//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System.Diagnostics.Contracts;
using System.Xml.Linq;
using JetBrains.Annotations;
using LargoCommon.Abstract;
using LargoCommon.Music;
namespace LargoCommon.Support
{
///
/// Score Part object.
///
public sealed class ScorePartObject
{
#region Properties
///
/// Gets or sets Id.
///
/// Property description.
public string Id { get; set; }
///
/// Gets or sets PartName.
///
/// Property description.
[UsedImplicitly]
public string PartName { get; set; } //// CA1044 (FxCop)
///
/// Gets or sets ScoreInstrumentId.
///
/// Property description.
public string ScoreInstrumentId { [UsedImplicitly] get; set; }
///
/// Gets or sets InstrumentName.
///
/// Property description.
public string InstrumentName { [UsedImplicitly] get; set; }
///
/// Gets or sets MidiInstrumentId.
///
/// Property description.
[UsedImplicitly]
public string MidiInstrumentId { get; set; } //// CA1044 (FxCop)
///
/// Gets or sets MidiChannel.
///
/// Property description.
public MidiChannel MidiChannel { get; set; }
///
/// Gets or sets MidiProgram.
///
/// Property description.
public byte MidiProgram { get; set; }
///
/// Gets or sets Volume.
///
/// Property description.
private string Volume { get; set; }
//// public byte? Volume { get; set; }
///
/// Gets or sets Pan.
///
/// Property description.
private byte? Pan { get; set; }
#endregion
#region Public static methods
///
/// Read Musical Part.
///
/// Score part.
/// Returns value.
public static ScorePartObject ReadScorePart(XElement scorePart) {
if (scorePart == null) {
return null;
}
var part = new ScorePartObject {
Id = (string)scorePart.Attribute("id"),
PartName = (string)scorePart.Element("part-name")
};
var si = scorePart.Element("score-instrument");
if (si != null) {
part.ScoreInstrumentId = (string)si.Attribute("id");
part.InstrumentName = (string)si.Element("instrument-name");
}
var mi = scorePart.Element("midi-instrument");
if (mi == null) {
return part;
}
part.MidiInstrumentId = (string)mi.Attribute("id");
part.MidiChannel = (MidiChannel)XmlSupport.ReadByteAttribute(mi.Attribute("midi-channel"));
part.MidiProgram = XmlSupport.ReadByteAttribute(mi.Attribute("midi-program"));
part.Volume = (string)mi.Element("volume"); //// (byte?)(int?)
part.Pan = (byte?)(int?)mi.Element("pan");
return part;
}
#endregion
#region Public methods
///
/// Score Part.
///
/// Returns value.
public XElement ScorePart() {
Contract.Requires(this.Id != null);
var scorePart = new XElement("score-part");
scorePart.Add(new XAttribute("id", this.Id));
scorePart.Add(new XElement("part-name", this.PartName));
/* Music Xml - Not used now.
XElement si = new XElement("score-instrument");
if (!string.IsNullOrEmpty(this.ScoreInstrumentId)) {
si.Add(new XAttribute("id", this.ScoreInstrumentId));
}
if (!string.IsNullOrEmpty(this.InstrumentName)) {
si.Add(new XAttribute("instrument-name", this.InstrumentName));
}
scorePart.Add(si);
*/
var mi = new XElement("midi-instrument");
if (!string.IsNullOrEmpty(this.MidiInstrumentId)) {
mi.Add(new XAttribute("id", this.MidiInstrumentId));
}
mi.Add(new XAttribute("midi-channel", (byte)this.MidiChannel));
mi.Add(new XAttribute("midi-program", this.MidiProgram));
mi.Add(new XElement("volume", this.Volume ?? string.Empty)); //// 0
mi.Add(new XElement("pan", this.Pan ?? 0));
scorePart.Add(mi);
return scorePart;
}
#endregion
}
}